/*
 *  Gurr - A multiprotocol chat client for Android
 *  Copyright (C) 2011 Malte Aschermann, Wilfried Ramaker
 *  
 *  This file is part of Gurr.
 *  
 *  Gurr is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  Gurr is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *  
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Gurr.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.masc.gurr.view.activities;

import java.util.List;

import com.masc.gurr.Controller;
import com.masc.gurr.R;
import com.masc.gurr.model.Contact;
import com.masc.gurr.view.adapter.ChatAdapter;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class ChatsActivity extends ListActivity {

	private List<Contact> chat_list;
	private ChatAdapter chat_adapter;

	public void update(List<Contact> chatListItems) {
		chat_list=chatListItems;
		chat_adapter = new ChatAdapter(this, R.layout.chat_list_row, chat_list);
		setListAdapter(chat_adapter);
	}
	
	public ChatsActivity() {
		// register activity
        Controller.getInstance().setChatsActivity(this);
        chat_list=Controller.getInstance().getChatItems();
	}
	
    @Override
	public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);

    	chat_adapter = new ChatAdapter(this, R.layout.chat_list_row, chat_list);

		setListAdapter(chat_adapter);

		ListView lv = getListView();
		lv.setTextFilterEnabled(true);

		lv.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View view, int arg2,
					long arg3) {
				// when clicked, tell 'Controller' to open chat window
				Controller.getInstance().chatWith(
						((TextView) view.findViewById(R.id.contact_uid)).getText().toString()
						);
				
			}
		});
	}
    
    
}
